package com.cloud.util;

import org.apache.commons.lang3.ArrayUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/21.
 */
public class DisMath {


    public static void main(String[] args) {
        List<Discount> dList = new ArrayList<Discount>();
        Discount d1  =new Discount();
        d1.setName("P1");
        d1.setItems(new String[]{"a", "b"});
        d1.setDisPrice(100);
        dList.add(d1);
        Discount d2  =new Discount();
        d2.setName("P2");
        d2.setItems(new String[]{"a", "b","c"});
        d2.setDisPrice(99);
        dList.add(d2);
        Discount d3  =new Discount();
        d3.setName("P3");
        d3.setItems(new String[]{"d", "e"});
        d3.setDisPrice(89);
        dList.add(d3);
        Discount d4  =new Discount();
        d4.setName("P4");
        d4.setItems(new String[]{"c", "d"});
        d4.setDisPrice(79);
        dList.add(d4);
        Discount d5  =new Discount();
        d5.setName("P5");
        d5.setItems(new String[]{"a", "e"});
        d5.setDisPrice(75);
        dList.add(d5);
        Discount d6  =new Discount();
        d6.setName("P6");
        d6.setItems(new String[]{"e", "f"});
        d6.setDisPrice(60);
        dList.add(d6);
        Discount d7  =new Discount();
        d7.setName("P7");
        d7.setItems(new String[]{"f", "g"});
        d7.setDisPrice(59);
        dList.add(d7);
        Discount d8  =new Discount();
        d8.setName("P8");
        d8.setItems(new String[]{"h", "g"});
        d8.setDisPrice(58);
        dList.add(d8);
        // 最优组合
        String mp="";
        // 最优组合的商品集合
        String[] ml=null;
        // 最优价格
        int mv=0;
        // 最后一个不互斥的优惠在list中所处的位置
//        int  localSize=0;


        for(int i=0;i<dList.size();i++){
            // 当前循环最优组合
            String mpn="";
            // 当前最优组合的商品集合
            String[] mln=null;
            // 当前最优价格
            int mvn=0;

            // 历史循环最优组合
            String mph="";
            // 历史最优组合的商品集合
            String[] mlh=null;
            // 历史最优价格
            int mvh=0;

            // 优惠组合
            String[] disItem = dList.get(i).getItems();
            String name =dList.get(i).getName();
            System.out.println(i);
            // 优惠价格
            int disValue = dList.get(i).getDisPrice();

            for(int j=i+1;j<dList.size();j++){
                String[] disItem1 = dList.get(j).getItems();
                        if(compare(disItem,disItem1)==true){
                            // 计算不互斥的2个优惠的总价
                            int valueTotal=disValue+dList.get(j).getDisPrice();
                            String name1 =dList.get(j).getName();
                            // 如果总价大于外面的最优价格 则覆盖
                            // 判断历史
                            if(mvh==0){
                                    mph=name+"+"+name1;
                                    mvh = valueTotal;
                                    mlh = (String[]) ArrayUtils.addAll(disItem, disItem1);
                            }else{
                                    mpn=name+"+"+name1;
                                    mvn = valueTotal;
                                    mln = (String[]) ArrayUtils.addAll(disItem, disItem1);
                            }

                            for(int k=j+1;k<dList.size();k++){
                                String[] disItem2 = dList.get(k).getItems();
                                String name2 =dList.get(k).getName();
                                if(mvn==0){
                                    if(compare(mlh,disItem2)==true){
                                        mph=mph+"+"+name2;
                                        mvh = mvh+dList.get(k).getDisPrice();
                                        mlh = (String[]) ArrayUtils.addAll(mlh, disItem2);
                                    }
                                }else{
                                    if(compare(mln,disItem2)==true){
                                        mpn=mpn+"+"+name2;
                                        mvn = mvn+dList.get(k).getDisPrice();
                                        mln = (String[]) ArrayUtils.addAll(mln, disItem2);
                                    }

                                    if(k==dList.size()-1){
                                        if(mvn>mvh){
                                            mph=mpn;
                                            mvh = mvn;
                                            mlh = mln;
                                            mp="";
                                            mvn=0;
                                            mln=null;
                                        }
                                    }
                                }

                            }


                        }
            }

            if(mvh>mv){
                mp=mph;
                mv = mvh;
                ml = ml;
            }
        }

        System.out.println("最优组合是:"+mp+",最优优惠价格是:"+mv);
    }

   static boolean compare(String[] fArr,String[] sArr) {
        boolean result = true;
        String[] shortArr = null;
        String[] longArr = null;
        if (fArr.length > sArr.length) {
            shortArr = sArr;
            longArr = fArr;
        } else {
            shortArr = fArr;
            longArr = sArr;
        }

        String longStr = "";
        for (int i = 0; i < longArr.length; i++) {
            longStr += (longArr[i]);
        }

        for (int j = 0; j < shortArr.length; j++) {
            if (longStr.contains(shortArr[j].toString())) {
                result =false;
            }
        }
        return result;
    }

}
